package com.onlineauction.beans;

import java.util.List;

import com.onlineauction.entities.Auction;
import com.onlineauction.entities.Bid;
import com.onlineauction.entities.Item;

public class ConfirmBidBean {

	private String username;
	private String itemIDString;
	private long itemID = -1;
	private Auction activeAuction;
	private Bid bestBid;
	
	private long auctionID = -1;
	private boolean havingActiveAuction = false;
	private String owner = "";
	private String itemName = "";
	private String category = "";	
	private String basePrice = "";
	private String bestPrice = "";
	private String endTime = "";

	public void setUsername(String username) {
		this.username = username;
	}

	public void setItemIDString(String itemIDString) {
		this.itemIDString = itemIDString;
		Long itemID = null;
		try {
			itemID = Long.parseLong(itemIDString);
		} catch (Exception e) {
			itemID = -1L;
		}
		this.itemID = itemID;
		fillDataFields();
	}

	public void fillDataFields() {

		if (this.itemID < 0) {
			return;
		}

		Item item = new Item(this.itemID);
		this.itemName = item.getProductName();
		this.category = item.getCategory().toString();
		this.owner = item.getOwner();

		List<Auction> auctions = Auction.getLiveAuctionsOfItem(itemID);
		if (auctions != null && auctions.size() > 0) {
			this.havingActiveAuction = true;
			this.activeAuction = auctions.get(0);
			this.basePrice = String.format("CAD$ %.2f", activeAuction.getAskPrice());
			this.endTime = activeAuction.getEndTime().toString();
			this.auctionID = activeAuction.getAuctionID();
			
			this.bestBid = Bid.getBestBidOfAuction(activeAuction.getAuctionID());
			if (this.bestBid != null) {
				this.bestPrice = String.format("CAD$ %.2f", this.bestBid.getBidPrice());
			} else {
				this.bestPrice = "No bids yet";
			}
		}
	}

	public String getItemName() {
		return itemName;
	}

	public String getCategory() {
		return category;
	}

	public boolean isHavingActiveAuction() {
		return havingActiveAuction;
	}

	public String getBasePrice() {
		return basePrice;
	}

	public String getBestPrice() {
		return bestPrice;
	}

	public String getEndTime() {
		return endTime;
	}

	public String getOwner() {
		return owner;
	}

	public long getItemID() {
		return itemID;
	}

	public void setItemID(long itemID) {
		this.itemID = itemID;
	}

	public Bid getBestBid() {
		return bestBid;
	}
	
	public String getNextBidPrice() {
		double price = 0.0;
		if (this.bestBid != null) {
			price = bestBid.getBidPrice() + 10.00;			
		} else if (this.activeAuction != null) {
			price = activeAuction.getAskPrice();
		} else {
			price = 1.00;
		}
		return String.format("%.2f", price);		
	}

	public long getAuctionID() {
		return auctionID;
	}
	
	
	
	
}
